package com.boho.common;

import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Utils {

	public static Long[] ObjArrayToLongArray(Object[] obj) throws Exception{
		Long[] longs = null;
		if(obj != null && obj.length > 0){
			int size = obj.length;
			longs = new Long[size];
			for(int i = 0; i < size; i++){
				longs[i] = Long.parseLong(obj[i].toString());
			}
		}
		return longs;
	}
	
	public static Integer[] ObjArrayToIntArray(Object[] obj) throws Exception{
		Integer[] ints = null;
		if(obj != null && obj.length > 0){
			int size = obj.length;
			ints = new Integer[size];
			for(int i = 0; i < size; i++){
				ints[i] = Integer.parseInt(obj[i].toString());
			}
		}
		return ints;
	}
	
	public static String[] ObjArrayToStringArray(Object[] obj) throws Exception{
		String[] str = null;
		if(obj != null && obj.length > 0){
			int size = obj.length;
			str = new String[size];
			for(int i = 0; i < size; i++){
				str[i] = String.valueOf(obj[i].toString());
			}
		}
		return str;
	}
	
	/**
	 * 返回指定格式的时间字符串
	 * @param format 时间格式
	 * @return
	 */
	public static String getDateTime(String format) {
		String strDateTime = "";
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		strDateTime = sdf.format(new Date());
		return strDateTime;
	}
	
	/**
	 * 判断是否是移动手机号码
	 * @param str
	 * @return
	 */
	public static boolean isMobile(String str) {
		Pattern pattern = Pattern.compile("^((\\(\\d{3}\\))|(\\d{3}\\-))?(13|15|18)\\d{9}$");
		Matcher matcher = pattern.matcher(str);
		return matcher.matches();
	}
	
	/**
	 * 判断是否是移动手机号码
	 * @param str
	 * @return
	 */
	public static boolean isMobile(String[] str){
		for(String temp:str){
			if(!isMobile(temp)){
				return false;
			}
		}
		return true;
	}
	
	/**
	 * 判断是否是URL
	 * @param str
	 * @return
	 */
	public static boolean isUrl(String str){
		Pattern pattern = Pattern.compile("^http:\\/\\/[A-Za-z0-9]+\\.[A-Za-z0-9]+[\\/=\\?%\\-&_~`@[\\\\]\\':+!]*([^<>\\\"\\\"])*$");
		Matcher matcher = pattern.matcher(str);
		return matcher.matches();
	}
	
	/**
	 * 判断字符串是否为空
	 * @param str
	 * @return
	 */
	public static boolean isBlank(String str){
		return str==null||str.trim().equals("");
	}
	
	/**
	 * 判断对象是否为空
	 * @param str
	 * @return
	 */
	public static boolean isBlank(Object str){
		return str==null;
	}
	
	/**
	 * 字符串按照指定长度截断，返回字符串数组
	 * @param str
	 * @param length
	 * @return
	 */
	public static String[] subStringToArray(String str,int length){
		int size=str.length()%length==0 ? 
				str.length()/length : str.length()/length+1;
		String[] array = new String[size];
		for(int i=0;i<size;i++){
			if(i!=size-1){
				array[i]=str.substring(i*length, i*length+length);
			}else{
				array[i]=str.substring(i*length);
			}
		}
		return array;
	}
	
	/**
	 * 字符串数组连接成字符串
	 * @param str
	 * @param link
	 * @return
	 */
	public static String arrayToString(String[] array,String link){
		StringBuffer sb=new StringBuffer();
		for(String temp : array){
			if(temp!=null&&!temp.equals("")){
				if(sb.length()>0){
					sb.append(link);
				}
				sb.append(temp);
			}
		}
		return sb.toString();
	}
	
	/**
	 * 压缩数组，删除null值和空字符串值
	 * @param array 数组
	 * @param split 连接符
	 * @param mergerSize 合并大小
	 * @return
	 */
	public static String[] compressSizeArray(String[] array,String split,int mergerSize){
		List<String> result=new ArrayList<String>();
		StringBuffer sb=new StringBuffer();
		int count=0;
		for(int i=0;i<array.length;i++){
			String temp=array[i];
			if(temp!=null&&!temp.equals("")){
				if(sb.length()>0){
					sb.append(split);
				}
				sb.append(temp);
				count++;
			}
			if((count%mergerSize==0 && count!=0) || i==array.length-1){
				result.add(sb.toString());
				sb=new StringBuffer();
			}
		}
		return result.toArray(new String[result.size()]);
	}
	
	/**
	 * 产生随机验证码
	 * @param count
	 * @return
	 */
	public static String getRandomCode(int count) {
		Random rand = new Random();
		String randNum = String.valueOf(Math.abs(rand.nextInt()));
		while (randNum.length() != count) {
			if (randNum.length() > count)
				randNum = randNum.substring(0, randNum.length() - 1);
			else
				randNum = randNum + "0";
		}
		return randNum;
	}
	
	/**
    * 字符串转换为日期
    * @param strDate 日期的字符串形式
    * @param format 转换格式
    * @return String 转换后的日期字符串
    */
    public static Date strToDate(String strDate, String format) throws Exception {
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        Date date = null;
        date = dateFormat.parse(strDate);
        return date;
    }
    /**
	 * 字符内码转换 ISO TO 制定编码
	 */
	public static String getISOtoEncode(String para, String encode) {
		if (para == null || "".equals(para)) {
			return "";
		}
		para = para.trim();
		String strtmp = "";
		try {
			strtmp = new String(para.getBytes("iso-8859-1"), encode);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return strtmp;
	}
	
	public static String lastSubstring(String source, int length){
		String no = source.substring(source.length() - length);
		return source.substring(0,source.length() - no.length());
	}
	
   	/**  
     * 删除input字符串中的html格式  
     *   
     * @param input  
     * @param length  
     * @return  
     */  
    public static String splitAndFilterString(String input, int length) {   
        if (input == null || input.trim().equals("")) {   
            return "";   
        }   
        // 去掉所有html元素,   
        String str = input.replaceAll("\\&[a-zA-Z]{1,10};", "").replaceAll(   
                "<[^>]*>", "");   
        str = str.replaceAll("[(/>)<]", "");   
        int len = str.length();   
        if (len <= length) {   
            return str;   
        } else {   
            str = str.substring(0, length);   
            str += "......";   
        }   
        return str;   
    }
    
    public static String setMessageContext(String errorContents, Object arg1){
      Object[] variables = { arg1 };
      return MessageFormat.format(errorContents, variables);
    }
	
    /**   
     *@param ms 为要转换的毫秒数   
     *@return 返回h m s数组   
     */   
    public static long[] toHMS(long ms){
    	long s;//秒
    	long h;//小时
    	long m;//分钟
    	h = ms/1000/60/60;
    	m = (ms-h*60*60*1000)/1000/60;
    	s = ms/1000-h*60*60-m*60;
    	return new long[]{h,m,s};
    }
	
	public static void main(String[] args) {
		long[] hms = Utils.toHMS(12312312312l);
		System.out.println(hms[0]+"小时"+hms[1]+"分"+hms[2]+"秒");
		System.out.println(Utils.setMessageContext("sssss{0}", "aa"));
	}
	
}
